<?php

namespace app\api\controller;

use app\api\model\Cart as CartModel;
use app\api\model\Goods as GoodsModel;
use app\common\service\qrcode\Goods as GoodsPoster;
use think\Db;
use app\common\model\PriceSetting as SettingModel;

/**
 * 商品控制器
 * Class Goods
 * @package app\api\controller
 */
class Goods extends Controller {
    /**
     * 商品列表
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function lists() {
        // 整理请求的参数
        $param = array_merge($this->request->param(), [
            'status' => 10,
            'province' => $this->loca['province'], // 城市
            'city' => $this->loca['city']
        ]);
        
        // 获取列表数据
        $model = new GoodsModel;
        $list  = $model->getList($param, $this->getUser(false));
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 获取商品详情
     * @param $goods_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail($goods_id) {
        // 用户信息
        $user = $this->getUser(false);
        // 商品详情
        $model = new GoodsModel;
        $goods = $model->getDetails($goods_id, $this->getUser(false));
        if ($goods === false) {
            return $this->renderError($model->getError() ?: '商品信息不存在');
        }
        $goods['collect'] = 0;
        if ($user) {
            $collect = Db::name('goods_collect')->where('goods_id', $goods_id)
                ->where('user_id', $user['user_id'])
                ->find();
            if ($collect) {
                $goods['collect'] = 1;
            }
        }
        // 多规格商品sku信息, todo: 已废弃 v1.1.25
        $specData = $goods['spec_type'] == 20 ? $model->getManySpecData($goods['spec_rel'], $goods['sku']) : null;
        return $this->renderSuccess([
            // 商品详情
            'detail'         => $goods,
            // 购物车商品总数量
            'cart_total_num' => $user ? (new CartModel($user))->getGoodsNum() : 0,
            // 多规格商品sku信息
            'specData'       => $specData,
        ]);
    }

    /**
     * 生成商品海报
     * @param $goods_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function poster($goods_id) {
        // 商品详情
        $detail = GoodsModel::detail($goods_id);
        $Qrcode = new GoodsPoster($detail, $this->getUser(false));
        return $this->renderSuccess([
            'qrcode' => $Qrcode->getImage(),
        ]);
    }

    /**
     * 价格区间
     */
    public function pricegrade()
    {
        $list = SettingModel::getItem('basic');
        $res = [];
        $index = 0;
        foreach ($list as $key => $value) {
            $res[$index] = $value;
            $index++;
        }
        return $this->renderSuccess([
            'grade' => $res,
        ]);
    }

}
